import yaml
import toml
import IPy


def load_yaml(yaml_file_path):
    with open(yaml_file_path) as f:
        yaml_data = yaml.load(f, yaml.FullLoader)
    return yaml_data


def load_toml(toml_file_path):
    with open(toml_file_path) as f:
        toml_data = toml.load(f)
    return toml_data


def is_ipaddress(ipstr, ipversion=4):
    try:
        IPy.parseAddress(ipstr, ipversion)
        return True
    except ValueError:
        return False


def recursive_update(default, custom):
    if not isinstance(default, dict) or not isinstance(custom, dict):
        raise TypeError('Params of recursive_update should be dicts')

    for key in custom:
        if isinstance(custom[key], dict) and isinstance(
                default.get(key), dict):
            default[key] = recursive_update(default[key], custom[key])
        else:
            default[key] = custom[key]

    return default
